package org.example.cotroller.ad;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Ad;
import org.example.entity.PageBean;
import org.example.entity.Result;
import org.example.service.AdService;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/ad")
public class adController {

    @Resource
    private AdService adService;
    /**
     * 按照条件分页查询广告
     * **/


    @GetMapping("")
    public Result<PageBean<Ad>> pageList(@RequestParam(value = "advId",required = false, defaultValue = "") Integer advId,
                                         @RequestParam(value = "title",required = false, defaultValue = "") String title,
                                         @RequestParam(value = "goodsName",required = false, defaultValue = "") String goodsName,
                                         @RequestParam(value = "status",required = false, defaultValue = "") String status,
                                         @RequestParam(value = "pageSize",required = false, defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "page",required = false, defaultValue = "1")Integer page){
        log.info("查询内容:{},{},{},{}",advId,goodsName,title,status);
        PageBean<Ad> categoryPageBean = adService.adByReason(pageSize,page,advId,goodsName,title,status);
        return Result.success(categoryPageBean);
    }

    /**
     * 插入广告
     * **/
    @PostMapping("")
    public Result insertAd(@RequestBody Ad ad){
        log.info("插入：{}",ad);
        adService.insertAd(ad);
        return Result.success();
    }
//    @PostMapping("")
//    public Result insertAd(@RequestBody String ad){
//        log.info("插入：{}",ad);
////        adService.insertAd(ad);
//        return Result.success();
//    }

    /**
     * 删除广告
     * **/
    @GetMapping("/{advId}")
    public Result deleteAd(@PathVariable Integer advId){
        log.info("删除id{}",advId);
        adService.deleteAd(advId);
        return Result.success();
    }

    /**
     * 编辑广告
     * **/
    @PostMapping("/{advId}")
    public Result updateAd(@RequestBody Ad ad,@PathVariable Integer advId){
        log.info("更新广告{},内容:{}",advId,ad);
        ad.setAdvId(advId);
        adService.updateAd(ad);
        return Result.success();
    }
}
